package graph;

import static ogl.vecmathimp.FactoryDefault.vecmath;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glViewport;

import ogl.app.App;
import ogl.app.Input;
import ogl.app.OpenGLApp;
import ogl.vecmath.Matrix;
import ogl.vecmathimp.MatrixImp;

import org.lwjgl.opengl.GL11;

import shader.Shader;

public class Scene implements App {
//	private List<Part> parts;
	private Group root;
	private static Shader defaultShader;

	// The attribute indices for the vertex data.
	public static int vertexAttribIdx = 0;
	public static int colorAttribIdx = 1;

	static public void main(String[] args) {
		new OpenGLApp("Rotating Cube - OpenGL ES 2.0 (lwjgl)", new Scene())
				.start();
		// OpenGL App ruft init() auf, dann simulate(), dann display(),
		// simulate(), display() etc.
	}

	public static Shader getDefaultShader() {
		return defaultShader;
	}

	@Override
	public void init() {
		defaultShader = new Shader();
		
		buildSceneGraph();
		
		// Set background color to black.
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

		// Enable depth testing.
		glEnable(GL11.GL_DEPTH_TEST);

		// Shader
		// s = new Shader();
		//
		// // Cube 1
		// cube1 = new Wuerfel(s, 0.5f);
		// cube1.setTranslationVector(vecmath.vector(0.8f, 0, 0));
		//
		// // Cube 2
		// cube2 = new Wuerfel(s, 0.3f);
		// Color[] c = new Color[]{
		// vecmath.color(1, 0, 1),
		// vecmath.color(1, 0, 1),
		// vecmath.color(1, 0, 1),
		// vecmath.color(1, 0, 1),
		// vecmath.color(0, 1, 1),
		// vecmath.color(0, 1, 1),
		// vecmath.color(0, 1, 1),
		// vecmath.color(0, 1, 1)
		// };
		// cube2.setColor(c);
		// cube2.setTranslationVector(vecmath.vector(-2.5f, 1.7f, 0));
		// cube2.setRotationVector(vecmath.vector(0, 0, 1));
	}

	@Override
	public void simulate(float elapsed, Input input) {
		root.simulate(elapsed, input);
	}

	@Override
	public void display(int width, int height) {
		// Adjust the the viewport to the actual window size. This makes the
		// rendered image fill the entire window.
		glViewport(0, 0, width, height);

		// Clear all buffers.
		glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

		// Assemble the transformation matrix that will be applied to all
		// vertices in the vertex shader.
		float aspect = (float) width / (float) height;

		// The perspective projection. Camera space to NDC.
		Matrix projectionMatrix = vecmath.perspectiveMatrix(60f, aspect, 0.1f,
				100f);
		defaultShader.setProjectionMatrixUniform(projectionMatrix);

		root.draw();
	}

	private void buildSceneGraph() {
		root = new Group();
		root.setMatrix(new MatrixImp(new float[]{1,0,0,0,
												  0,1,0,0,
												  0,0,1,0,
												  0,0,0,1}));
		Wuerfel w = new Wuerfel();
		root.addPart(w);
	}

}

